package com.zrlh.file_trans_monitor.controller;

import com.alibaba.fastjson.JSONObject;
import com.zrlh.file_trans_monitor.entity.TaskConfEntity;
import com.zrlh.file_trans_monitor.service.FileMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class FileMonitorController {
    @Autowired
    private FileMonitorService fileMonitorService;
    @ResponseBody
    @RequestMapping(value="/testPOST",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public void testPOST(@RequestBody String json){
        System.out.println("JSON FROM: "+json);
    }
    @ResponseBody
    @RequestMapping(value = "/getData", method = RequestMethod.GET)
    public JSONObject getData(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        JSONObject result = new JSONObject();
        /*fileMonitorService.setSTATUS();*/
        List<TaskConfEntity> list=new ArrayList<>();
        int size=fileMonitorService.getTaskConfEntityList().size();
        int start=(pageNum-1)*pageSize;
        int end;
        if(size<pageNum*pageSize)
            end=size;
        else
            end=pageNum*pageSize;
        for(int i=start;i<end;i++){
            list.add(fileMonitorService.getTaskConfEntityList().get(i));
        }
        try {
            result.put("code", "0");
            result.put("msg", "操作成功！");
            result.put("data", list);
            result.put("count", size);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "查询异常！");
        }
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "/getErrorData", method = RequestMethod.GET)
    public JSONObject getExceptionData(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        JSONObject result = new JSONObject();
        /*fileMonitorService.setSTATUS();*/
        List<TaskConfEntity> list=new ArrayList<>();
        fileMonitorService.sortErrorList();
        int size=fileMonitorService.getERRORList().size();
        int start=(pageNum-1)*pageSize;
        int end;
        if(size<pageNum*pageSize)
            end=size;
        else
            end=pageNum*pageSize;
        for(int i=start;i<end;i++){
            list.add(fileMonitorService.getERRORList().get(i));
        }
        try {
            result.put("code", "0");
            result.put("msg", "操作成功！");
            result.put("data", list);
            result.put("count", size);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "查询异常！");
        }
        return result;
    }

}
